import { createStore } from 'vuex'
import axios from 'axios'

const store = createStore({
  state() {
    return {
      count: 0,
      list: [],
      category: [],
      isShowMap: localStorage.getItem('isShowMap') ? (localStorage.getItem('isShowMap') === 'true' ? true : false) : true
    }
  },

  mutations: {
    addCount(state) {
      state.count += 1
    },

    setList(state, list) {
      state.list = list
    },

    setCate(state, cate) {
      state.category = cate
    },

    setShowMap(state, isShowMap) {
      state.isShowMap = isShowMap
      localStorage.setItem('isShowMap', isShowMap)
    }
  },

  actions: {
    add({ commit }) {
      commit('addCount')
    },
    minus() {
      console.log(1)
    },

    async loadData({ commit }) {
      let list = await axios.get('/api/list')
      commit('setList', list.data.data)
    },

    async loadCateData({ commit }) {
      let list = await axios.get('/api/hotcate')
      commit('setCate', list.data)
    },

    changeTabs({ commit }, isShowMap) {
      commit('setShowMap', isShowMap)
    }
  }
})

export default store